<!DOCTYPE html>
<#assign secSelect = JspTaglibs["http://www.casic304.cn/secSelect"] />
<html>
<head>
    <meta charset="utf-8">
    <title>自定义导出</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/qbdmsStatic/iconfont/iconfont.css"/>
    <!--<link rel="stylesheet" href="${ctx}/qbdmsStatic/style/forms/currencyCss.css">-->
    <link rel="stylesheet" href="${ctx}/qbdmsStatic/style/qbdms.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
    <style>
        #lay-content{
            float:left;height: 343px;overflow: auto;padding-left: 13px;width: 100%;
        }
        /*body.layui-layout-body{overflow: auto}*/
    </style>
</head>
<body>

<script type="text/html" template>
<form class="layui-form layui-row" style="padding: 10px" lay-filter="wCustomColumnsDataForm">
    <div class="qbdms-oneForm" style="width: 100%">
        <label class="layui-form-label qbdms-label">密级<span style="color: red;">*</span></label>
        <div class="layui-input-block qbdms-input">
            <@secSelect.sec_select id="secret" data_src=1/>
        </div>
    </div>
    <div id="lay-content"> </div>
</form>
</script>
<script src="${ctx}/qbdmsStatic/public/configFile.js"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>
    layui.config({
        base: serverPath.qbdmsPath + staticPath.qbdmsPath //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','common','jquery','transfer','form'], function () {
        var $ = layui.jquery;
        var common = layui.common;
        var transfer = layui.transfer;
        var form = layui.form;
        var layContent = $("#lay-content");
        var transfers = ".demo-transfer";

        var tableInfoJson = JSON.parse('${tableInfo}');
        var mainTableName = tableInfoJson.tableName;
        var mainTableModelName = tableInfoJson.tableModelName;
        var mainSubTable = tableInfoJson.subTable;
        var fields = tableInfoJson.fields;
        var htm = '<div id="'+mainTableModelName+'" class="demo-transfer" style="margin-bottom: 10px;" sign="main"></div>';
        mainSubTable && $.each(mainSubTable,function(i,n){
            htm += '<div id="'+n.tableModelName+'" class="demo-transfer" sign="sub"></div>';
        });
        layContent.html(htm);

        renderTransfer(mainTableName,mainTableModelName,fields,true);
        mainSubTable && $.each(mainSubTable,function(i,n){
            renderTransfer(n.tableName,n.tableModelName,n.fields,false);
        });

        $(transfers).each(function(i,n){
            var cid = $(n).attr("id");
            addTop(cid);
        });

        function renderTransfer(leftName , elem , data , isMain){
            var checkData = [];
            $.each(data,function(i,n){
                checkData.push(n.key)
            });
            //实例调用
            transfer.render({
                elem: '#'+elem
                ,title: [leftName+(isMain?'(主表)':'(子表)'), '导出字段']
                ,data: data
                ,parseData: function(allData){
                    return  {
                        "value": allData.key //数据值
                        ,"title": allData.value //数据标题
                        ,"disabled": false  //是否禁用
                        ,"checked": false  //是否选中
                    }
                }
                ,value: checkData
                ,id: elem //定义唯一索引
                ,height:(mainSubTable&&mainSubTable.length>0)?255:490
                ,width:277
                ,showSearch: true
                ,onchange:function(data, index){
                    if(index === 0){
                        addTop(elem);
                    }else{
                        $('#'+elem+' .layui-transfer-box:eq(0) .tranMove').remove();
                    }
                }
            });

        }

        function addTop(elem){
            $('#'+elem+' .layui-transfer-box:eq(1) ul li').each(function (i,item) {
                $(item).append('<i class="tranMove layui-icon layui-icon-upload-circle"></i>')
            });
            $('#'+elem+' .tranMove').click(function () {
                var _tP = $(this).parent();
                _tP.prev().before(_tP);
            });
        }

        getData =  function () {
            if($(transfers).length===0){
                return false;
             }
            var cAllData = {},
                cDataArr = [],
                curtId,
                curtSign,
                cData = {},
                showData,
                sign = true;
            var secretText = $('#secret :selected').text();
            var secretVal = $('#secret').val();
            if(!secretVal){
                layer.alert("导出失败，请选择密级！",{title: false,skin: 'confirm-class'});
                return false;
            }
            cAllData.secret = secretVal;
            $(transfers).each(function(i,n){
                cData = {};
                curtId = $(n).attr("id");
                curtSign = $(n).attr("sign");
                showData = transfer.getData(curtId);
                if(curtSign === 'main'){
                    cData.isMain = true;
                    cData.secret = secretText;
                    if(showData.length<1){
                        layer.alert("导出失败，主表至少要导出一列！",{title: false,skin: 'confirm-class'});
                        sign = false;
                        return false;
                    }
                }else{
                    cData.isMain = false;
                }
                cData.modelName = curtId;
                var showField = "";
                $.each(showData,function(i,n){
                    showField += n.value + ",";
                });
                showField && (showField = showField.substr(0,showField.length-1));
                cData.fields = showField;
                if(showField){
                    cDataArr.push(cData);
                }
            });
            cAllData.dataArray = cDataArr;
            return sign?cAllData:sign;
        }
    });
</script>
<style>
    #lay-content li{
        position: relative;
    }
    #lay-content .tranMove{
        position: absolute;
        right: 3px;
        cursor: pointer;
    }
</style>
</body>
</html>